package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 执行聚合函数
 */
public class JDBCDemo7 {
    public static void main(String[] args) {
        //需求：查看1年级1班共多少人？
        /**
         *SELECT COUNT(*) count
         * FROM student  s
         * JOIN class c ON s.class_id = c.id
         * WHERE c.name = '1年级1班'
         */
        //1.建立连接
        try(
                Connection conn = DBUtil.getConnection();
        ) {
            //2.创建Statement对象
            Statement state = conn.createStatement();
            //3.执行SQL语句
            String sql = "SELECT COUNT(*) count " +
                         " FROM student  s " +
                         "JOIN class c ON s.class_id = c.id " +
                         "WHERE c.name = '1年级1班'";
            //4.处理结果
            ResultSet rs = state.executeQuery(sql);
            if (rs.next()){
                int count = rs.getInt("count");
                System.out.println("共"+count+"人");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
